<?php

$sessao = new Zend_Session_Namespace('Parametros');
?>
<div class="content-form">
    <h2><?php echo $this->titulo; ?></h2>
    <form id="form_pdr" action="" method="post" enctype="multipart/form-data">    
            <input type="hidden" id="LAP_ID" name="LAP_ID" value="<?php echo $this->data['LAP_ID']; ?>" />
            
            <div class="col">
                <div class="ln"><strong>Instituição de Ensino</strong>
                <?php
                    $html = '<select name="FAC_ID[]" class="chosen-select"><option value="0">Nenhum</option>'; // multiple="multiple" style="width: 400px;min-height: 70px;">
                    $query = 'SELECT SQL_CACHE FAC_ID,FAC_NOM FROM FACULDADE WHERE FAC_STA = 1 ORDER BY FAC_NOM ASC';
                    $row = $this->db->fetchAll($query);
                    $arr = array();
                    foreach($row as $r){ 
                        $selected = $r['FAC_ID'] == $this->data['FAC_ID'] ? 'selected=""':'';            
                        $html .= '<option '.$selected.' value="'.$r['FAC_ID'].'">'.$r['FAC_NOM'].'</option>'; 
                    }
                    echo $html .= '</select>';
                ?>
                </div>
                
                <div class="ln"><strong>Ano</strong>
                    <input type="text" name="LAP_ANO" value="<?php echo $this->data['LAP_ANO']; ?>" required="" />
                </div>
                <div class="ln"><strong>Semestre</strong>
                    <input type="text" name="LAP_SEM" value="<?php echo $this->data['LAP_SEM']; ?>" required="" />
                </div>
                <div class="ln"><strong>Chamada</strong>
                    <input type="text" name="LAP_CHM" value="<?php echo $this->data['LAP_CHM']; ?>" required="" />
                </div>
                <?php //if($this->data['LAP_STA'] < 2 && count($this->excel) == 0){ ?>
                <div class="ln"><strong>Arquivo</strong>
                    <input type="file" name="LAP_FILE" value="<?php echo $this->data['LAP_FILE']; ?>"  />
                </div>
                <?php //} ?>
                
                <div class="ln content-list">
                <?php if(count($this->excel) > 0) { ?>
                 <table style="width:800px">
                        <tr>
                            <th width="250px">Aluno</th>
                            <th width="200px">Curso</th>
                            <th width="250px">Alunos Sistema</th>
                        </tr>
                <?php 
                    
                    $_curso = '';
                    
                    foreach($this->excel as $key => $excel){
                    
                        $excel[1] = trim($excel[1]);
                        
                        if(strlen($excel[2]) > 3){
                            $_n_curso = $excel[2];
                            $_curso = '<input type="hidden" name="cursos['.$key.']" value="'.$excel[2].'" />'.$excel[2];
                        } else if(strlen($_n_curso) > 3 && strlen($_curso) > 3) {
                            $_curso = '<input type="hidden" name="cursos['.$key.']" value="'.$_n_curso.'" />'.$_n_curso;
                        }
                        
                        
                        if(strlen($_curso) < 2){

                            $html = '<select id="cursos_'.$key.'" name="cursos['.$key.']">';
                            $html .= '<option value="">Nenhum</option>';
                            foreach($this->cursos as $cursos){
                                $html .= '<option value="'.$cursos['CUF_ID'].'">'.$cursos['CUF_NOM'].'</option>';
                            }
                            $html .= '</select>';
                            $_curso = $html;
                        }
                    
                    
                    
                    ?>
                    
                    <?php if(strlen($excel[1]) > 1){ 
                        
                            $not_in = '0';
                            $listar = false;
                            
                            $where = '';
                            if($this->data['LAP_ID'] > 0) $where = ' AND p.LAP_ID = '.$this->data['LAP_ID'];
                        
                            $query = 'SELECT SQL_CACHE a.ALU_ID, a.ALU_NOM, p.APV_ID, p.CUF_ID FROM ALUNO a '
                                    . ' LEFT JOIN APROVACAO p ON p.ALU_ID = a.ALU_ID '
                                    . 'WHERE p.APV_EXCNOM like "%'.$excel[1].'%" '
                                    //. 'WHERE MATCH(a.ALU_NOM) AGAINST ("'.$excel[2].'") '
                                    . $where;
                            $tots = $this->db->fetchAll($query);


                            foreach($tots as $row){ 
                                
                                $not_in .= ','.$row['ALU_ID'];
                                
                                $listar = true;
                                break;
                            }
                       
                            if($listar== false){
                                $query = 'SELECT SQL_CACHE a.ALU_ID, a.ALU_NOM FROM ALUNO a '
                                . 'WHERE  a.ALU_ID NOT IN('.$not_in.')'
                                ;//. 'AND a.ALU_NOM like "%'.$excel[2].'%"';
                                $tots = $this->db->fetchAll($query);

                                foreach($tots as $row){ 

                                    similar_text(strtolower($excel[1]),strtolower($row['ALU_NOM']),$per);

                                    if($per >= $sessao->parametros['PAR_PERSEM']){
                                        $listar = true;
                                        break;
                                    }
                                }
                            }
                            
                        if($listar){
                    ?>
                    
                        <tr>
                            <td><?php echo $excel[1]; ?></td>  
                            <td><?php echo $_curso; ?></td>
                            <td>
                                <?php
                                    $where = '';
                                    
                                    if($this->data['LAP_ID'] > 0) $where = ' AND p.LAP_ID = '.$this->data['LAP_ID'];
                                    
                                    if($this->data['LAP_STA'] == 2) {
                                        
                                        //$query = 'SELECT SQL_CACHE CUF_ID FROM CURSOFACULDADE WHERE CUF_NOM LIKE "%'.$excel[1].'%"';
                                        //$r_cur = $this->db->fetchRow($query);
                                        
                                        //if($r_cur['CUF_ID'] > 0 ) $where .= ' AND p.CUF_ID = '.$r_cur['CUF_ID'];
                                        
                                    }
                                    
                                    $query = 'SELECT SQL_CACHE a.ALU_ID, a.ALU_NOM, p.APV_ID, p.CUF_ID FROM ALUNO a '
                                            . ' LEFT JOIN APROVACAO p ON p.ALU_ID = a.ALU_ID '
                                            . 'WHERE p.APV_EXCNOM like "%'.$excel[1].'%" '
                                            //. 'WHERE MATCH(a.ALU_NOM) AGAINST ("'.$excel[2].'") '
                                            . $where;
                                    $result = $this->db->fetchAll($query);
                                    
                                    
                                    $not_in = '0';
                                    $style = '';
                                ?>
                                <table style="width: 300px;">
                                    <?php foreach($result as $row){ 
                                         $style = ($row['APV_ID'] > 0) ? 'style="display:none;"':'';
                                         
                                         $not_in .= ','.$row['ALU_ID'];
                                    ?>
                                    <tr>
                                        <td>
                                            <label>
                                                <input type="radio" id="a_<?php echo $row['APV_ID']; ?>" class="lista_<?php echo $key; ?> alu_<?php echo $row['ALU_ID']; ?>" classe="alu_<?php echo $row['ALU_ID']; ?>" name="lista[<?php echo $key; ?>]" value="<?php echo $row['ALU_ID']; ?>" <?php echo $style; ?>/>
                                                
                                                <input type="hidden" name="exc_nom[<?php echo $key; ?>]" value="<?php echo $excel[1]; ?>" />
                                                
                                                <?php if($row['APV_ID'] > 0){ ?>
                                                <a href="javascript:void(0)" class="rem_apv" itemid="<?php echo $row['ALU_ID']; ?>" itemref="<?php echo $row['APV_ID']; ?>" itemprop="<?php echo $key; ?>" >[X]</a> 
                                                <?php }  ?>
                                                
                                                <?php echo $row['ALU_NOM'].' - '.strtoupper(aluno::returnStatusAluno($row['ALU_ID'])); ?>
                                            </label>
                                            <?php
                                                if($row['CUF_ID'] > 0) echo '<script>$(function(){ $("#cursos_'.$key.'").val('.$row['CUF_ID'].'); });</script>';
                                            ?>
                                        </td>
                                        <td></td>
                                    </tr>
                                    <?php } ?>
                                    
                                    <?php
                                        
                                        $query = 'SELECT SQL_CACHE a.ALU_ID, a.ALU_NOM FROM ALUNO a '
                                        . 'WHERE  a.ALU_ID NOT IN('.$not_in.')'
                                        ;//. 'AND a.ALU_NOM like "%'.$excel[2].'%"';
                                        $result = $this->db->fetchAll($query);
                                    ?>
                                    
                                    <?php foreach($result as $row){ 
                                        
                                        similar_text(strtolower($excel[1]),strtolower($row['ALU_NOM']),$per);
                                        
                                        if($per >= $sessao->parametros['PAR_PERSEM']){ // 90
                                    ?>
                                    <tr>
                                        <td>
                                            <label>
                                                <input type="radio" class="lista_<?php echo $key; ?> alu_<?php echo $row['ALU_ID']; ?>" classe="alu_<?php echo $row['ALU_ID']; ?>" name="lista[<?php echo $key; ?>]" value="<?php echo $row['ALU_ID']; ?>" <?php echo $style; ?>/>
                                                
                                                <input type="hidden" name="exc_nom[<?php echo $key; ?>]" value="<?php echo $excel[1]; ?>" />
                                                
                                                <?php echo $row['ALU_NOM'].' - '.strtoupper(aluno::returnStatusAluno($row['ALU_ID'])); ?>
                                            </label>
                                        </td>
                                        <td></td>
                                    </tr>
                                    <?php } 
                                    
                                        }
                                    ?>
                                    
                                </table>
                            </td>
                        </tr>
                        <?php } ?>
                <?php } } ?>
                    </table>
                <?php } ?>
                
                <?php /*if(count($this->lista) > 0){?>
                    <table style="width:450px">
                        <tr>
                            <th width="250px">Aluno</th>
                            <th width="200px">Curso</th>
                        </tr>
                    <?php foreach($this->lista as $lista){?>
                        <tr>
                            <td><?php echo $lista['ALU_NOM']; ?></td>  
                            <td><?php echo $lista['CUF_NOM']; ?></td>
                        </tr>
                    
                    <?php } ?>
                    </table>
                <?php }*/ ?>
                </div>
                
            </div>
            
            <div>
                <?php //if($this->data['LAP_STA'] < 2){ ?>
                <input type="submit" name="submit" id="submitbutton" value="Salvar" class="btn-frm">
                <?php //} ?>
                <a href="<?php echo $this->urlmodulo.'/'.$this->controller; ?>" class="btn-frm">voltar</a>
            </div>
        </form>
</div>